/*
    Copyright (C) 2011 Rémi Thebault

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>

#include "ganttscene.h"
#include "ganttitem.h"
#include "ganttcycle.h"
#include "procycle.h"


GanttItem::GanttItem(ProCycle* app, GanttCycle* gCycle, QGraphicsItem* parent) :
	QGraphicsItem(parent),
	PCChild(app),
	m_gCycle(gCycle),
	m_hovered(false),
	m_clickable(false)
{
	setFlag(QGraphicsItem::ItemIsSelectable);
}


GanttItem::~GanttItem() {

}


GanttScene* GanttItem::gScene() {
	return app()->ganttScene();
}


void GanttItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
	setHovered(pointCanHover(event->pos()));
}


void GanttItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
	setHovered(false);
}


void GanttItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
	if(pointCanSelect(event->pos())) {
		m_clickable = true;
		m_pressPos = event->pos();
	}
	else {
		m_clickable = false;
	}
}


void GanttItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
	if (m_clickable) {
		if ((event->pos() - m_pressPos).manhattanLength() >= 2.f) {
			m_clickable = false;
		}
	}
}


void GanttItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
	if (m_clickable) {
		if (pointCanSelect(event->pos())) {
			requestSelection(!isSelected());
		}
		else {
			if (isSelected()) {
				requestSelection(false);
			}
		}
	}
}
